/**
 * key-list部分.
 */
Vue.component("key-list", {
    template :
        `
        <div style="overflow: hidden;height: 100%;">
            <Tree :data="redisDbs" @on-select-change="selectDb" style="overflow-y: auto;overflow-x: hidden;height: 99%;"></Tree>
            <Spin fix v-if="loadingShow">
                <Icon type="load-c" size=18 style="animation: ani-demo-spin 1s linear infinite;"></Icon>
                <div>Loading</div>
            </Spin>
            <Spin fix v-if="alertShow">
                <Icon type="arrow-up-a" size="72"></Icon>
                <div>请先选择一个redis链接...</div>
            </Spin>
        </div>
        `,
    props : ["redisLink","keySearchValue"],
    data : function () {
        return {
            redisDbs : [],
            loadingShow : false,
            alertShow : true
        };
    },
    methods : {
        queryKeys : function (value) {
            if(this.redisLink == ""){
                this.$Notice.info({
                    title: "请先选择redis服务地址..."
                });
                return false;
            }
            if(value == ""){
                this.$Notice.info({
                    title: "请输入查询关键字..."
                });
                return false;
            }
            var that = this;
            that.loadingShow = true;
            axios.get("./queryKey", {params : {host : this.redisLink, key : this.keySearchValue}})
                .then(function (response) {
                    that.loadingShow = false;
                    var dbs = response.data, db, dbId, keys, dbsArray = [];
                    for(var i = 0;i < dbs.length;i++){
                        db = dbs[i];
                        dbId = db.db.split(",");
                        keys = db.keys;
                        var children = [];
                        if(keys && keys.length > 0){
                            var size = keys.length;
                            for(var j = 0;j < size;j++){
                                children.push({
                                    title : keys[j],
                                    dbIndex : dbId[0],
                                    expand: false
                                })
                            }
                        }
                        dbsArray.push({
                            title : "db_" + dbId[0] + "(" + dbId[1] + ")",
                            expand : true,
                            children : children,
                            dbIndex : dbId[0],
                            num: dbId[1],
                            isDb: true
                        });
                    }
                    that.redisDbs = dbsArray;
                })
                .catch(function (error) {
                    that.loadingShow = false;
                    that.$Notice.error({
                        title: "key查询失败...",
                        desc: error
                    });
                });
        },
        selectDb : function () {
            var that = this, data = arguments[0], obj;
            if(data && data.length > 0){
                obj = data[0];
                if(obj.isDb == true){
                    if(!obj.children || obj.children.length == 0){
                        that.loadingShow = true;
                        axios.get("./getDbKeys", {params : {host : this.redisLink, dbIndex : obj.dbIndex}})
                            .then(function (response) {
                                that.loadingShow = false;
                                var keys = response.data, array = [], size = keys.length;
                                for(var j = 0;j < size;j++){
                                    array.push({
                                        title : keys[j],
                                        dbIndex : obj.dbIndex,
                                        expand: false
                                    })
                                }
                                if(obj.num > size){
                                    array.push({
                                        title : "更多key请使用查询功能...",
                                        dbIndex : obj.dbIndex,
                                        expand: false,
                                        disabled : true
                                    });
                                }
                                obj.children = array;
                            })
                            .catch(function (error) {
                                that.loadingShow = false;
                                that.$Notice.error({
                                    title: "key查询失败...",
                                    desc: error
                                });
                            });
                    }else{
                        obj.expand = !obj.expand;
                    }
                }else {
                    this.$emit("selectKey", obj.dbIndex, obj.title);
                }
            }
        },
        refreshDbs : function (value, start, end, error) {
            if(value){
                var that = this;
                start();
                axios.get("./getRedisInfo", {params : {param : value}})
                    .then(function (response) {
                        end();
                        that.alertShow = false;
                        var dbs = response.data, t;
                        var dbsArray = [];
                        for(var i = 0;i < dbs.length;i++){
                            t = dbs[i].split(",");
                            dbsArray.push({
                                title : "db_" + t[0] + "(" + t[1] + ")",
                                expand : true,
                                children : [],
                                dbIndex : t[0],
                                num: t[1],
                                isDb: true
                            });
                        }
                        that.redisDbs = dbsArray;
                    })
                    .catch(function (e) {
                        error();
                        that.alertShow = true;
                        that.$Notice.error({
                            title: "redis服务连接失败...",
                            desc: e
                        });
                        that.redisDbs = [];
                    });
            }
        }
    },
    watch : {
        redisLink : function (value) {
            var that  = this;
            that.$Loading.config({
                height: 5
            });
            that.refreshDbs(value, function () {
                that.$Loading.start();
            }, function () {
                that.$Loading.finish();
            }, function () {
                that.$Loading.error();
            });
        },
        keySearchValue: function (value) {
            var that  = this;
            if(value){
                that.queryKeys(value);
            }else{
                that.refreshDbs(this.redisLink, function () {
                    that.loadingShow = true;
                }, function () {
                    that.loadingShow = false;
                }, function () {
                    that.loadingShow = false;
                });
            }
        }
    }
});